{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1aa104a6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.5708\n",
      "0.7854\n"
     ]
    }
   ],
   "source": [
    "#例5-9、练习5-7\n",
    "import numpy as np\n",
    "a=np.array([2,1,3,2])\n",
    "b=np.array([1,2,-2,1])\n",
    "cos=np.dot(a,b)/(np.linalg.norm(a)*np.linalg.norm(b))\n",
    "print('%.4f'%np.arccos(cos))\n",
    "a=np.array([1,2,2,3])\n",
    "b=np.array([3,1,5,1])\n",
    "cos=np.dot(a,b)/(np.linalg.norm(a)*np.linalg.norm(b))\n",
    "print('%.4f'%np.arccos(cos))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3a05644d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-1.  0.  1.]\n"
     ]
    }
   ],
   "source": [
    "#例5-10\n",
    "import numpy as np\n",
    "from utility import mySolve\n",
    "a1=np.array([1,1,1])\n",
    "a2=np.array([1,-2,1])\n",
    "A=np.vstack((a1,a2))\n",
    "o=np.zeros((2,1))\n",
    "X=mySolve(A,o)\n",
    "print(X[:,1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "8fd90b35",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 1 1] [-1.  1.  0.] [-0.5 -0.5  1. ]\n"
     ]
    }
   ],
   "source": [
    "#练习5-8\n",
    "import numpy as np\n",
    "from utility import mySolve\n",
    "a1=np.array([1,1,1])\n",
    "A=np.array([a1])\n",
    "o=np.zeros((1,1))\n",
    "X=mySolve(A,o)\n",
    "a2=X[:,1]\n",
    "A=np.vstack((A,a2))\n",
    "o=np.zeros((2,1))\n",
    "X=mySolve(A,o)\n",
    "a3=X[:,1]\n",
    "print(a1,a2,a3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "1b7b48b3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1.      0.3333 -0.2   ]\n",
      " [ 0.     -1.      0.6   ]\n",
      " [-1.      0.6667  0.6   ]\n",
      " [ 1.      0.3333  0.8   ]]\n",
      "[[ 0.5774  0.2582 -0.169 ]\n",
      " [ 0.     -0.7746  0.5071]\n",
      " [-0.5774  0.5164  0.5071]\n",
      " [ 0.5774  0.2582  0.6761]]\n"
     ]
    }
   ],
   "source": [
    "#例5-11\n",
    "import numpy as np\n",
    "from utility import orthogonalize,unitization\n",
    "np.set_printoptions(precision=4, suppress=True)\n",
    "A=np.array([[1,1,-1],\n",
    "            [0,-1,1],\n",
    "            [-1,0,1],\n",
    "            [1,1,0]],dtype='float')\n",
    "B=orthogonalize(A)\n",
    "print(B)\n",
    "unitization(B)\n",
    "print(B)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "845f7ecc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.5774 -0.7071  0.4082]\n",
      " [ 0.5774  0.     -0.8165]\n",
      " [ 0.5774  0.7071  0.4082]]\n",
      "[[ 1.  0. -0.]\n",
      " [ 0.  1.  0.]\n",
      " [-0.  0.  1.]]\n"
     ]
    }
   ],
   "source": [
    "#练习5-9\n",
    "import numpy as np\n",
    "from utility import orthogonalize,unitization\n",
    "np.set_printoptions(precision=4, suppress=True)\n",
    "A=np.array([[1,1,1],\n",
    "            [1,2,4],\n",
    "            [1,3,9]],dtype='float')\n",
    "B=orthogonalize(A)\n",
    "unitization(B)\n",
    "print(B)\n",
    "print(np.matmul(B,B.T))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "99ac2049",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1.  1. 10.]\n",
      "[[-0.9314 -0.1464 -0.3333]\n",
      " [ 0.1231  0.7351 -0.6667]\n",
      " [-0.3426  0.6619  0.6667]]\n",
      "[[ 1. -0. -0.]\n",
      " [ 0.  1.  0.]\n",
      " [-0.  0. 10.]]\n"
     ]
    }
   ],
   "source": [
    "#例5-14\n",
    "import numpy as np\n",
    "np.set_printoptions(precision=4, suppress=True)\n",
    "A=np.array([[2,2,-2],\n",
    "            [2,5,-4],\n",
    "            [-2,-4,5]])\n",
    "v,P=np.linalg.eigh(A)\n",
    "print(v)\n",
    "print(P)\n",
    "print(np.matmul(np.matmul(P.T,A),P))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a61abf39",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-2 1 4]\n",
      "[[-1/3 2/3 -2/3]\n",
      " [-2/3 1/3 2/3]\n",
      " [-2/3 -2/3 -1/3]]\n",
      "[[-2 0 0]\n",
      " [0 1 0]\n",
      " [0 0 4]]\n"
     ]
    }
   ],
   "source": [
    "#练习5-12\n",
    "import numpy as np\n",
    "from utility import mySolve,orthogonalize,unitization\n",
    "from utility import Q\n",
    "np.set_printoptions(formatter={'all':lambda x:\n",
    "                               str(Q(x).limit_denominator())})\n",
    "A=np.array([[2,-2,0],\n",
    "            [-2,1,-2],\n",
    "            [0,-2,0]])\n",
    "v,P=np.linalg.eigh(A)\n",
    "print(v)\n",
    "print(P)\n",
    "print(np.matmul(np.matmul(P.T,A),P))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d990b236",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 1]\n",
      " [2 4 2]\n",
      " [1 2 1]]\n"
     ]
    }
   ],
   "source": [
    "#例5-19\n",
    "import numpy as np\n",
    "from utility import symmetrization\n",
    "A=np.array([[1,4,2],\n",
    "            [0,4,4],\n",
    "            [0,0,1]])\n",
    "symmetrization(A)\n",
    "print(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "16b59722",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1 -1 -2]\n",
      " [-1  1 -2]\n",
      " [-2 -2 -7]]\n"
     ]
    }
   ],
   "source": [
    "#练习5-17\n",
    "import numpy as np\n",
    "from utility import symmetrization\n",
    "A=np.array([[1,-2,-4],\n",
    "            [0,1,-4],\n",
    "            [0,0,-7]])\n",
    "symmetrization(A)\n",
    "print(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "60db5c49",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-2.  1.  1.]\n",
      "[[-0.5774 -0.4225  0.6987]\n",
      " [-0.5774  0.8163  0.0166]\n",
      " [ 0.5774  0.3938  0.7152]]\n",
      "[[-2. -0.  0.]\n",
      " [-0.  1.  0.]\n",
      " [ 0.  0.  1.]]\n"
     ]
    }
   ],
   "source": [
    "#例5-20\n",
    "import numpy as np\n",
    "from utility import symmetrization\n",
    "np.set_printoptions(precision=4, suppress=True)\n",
    "A=np.array([[0,-2,2],\n",
    "            [0,0,2],\n",
    "            [0,0,0]])\n",
    "symmetrization(A)\n",
    "v,P=np.linalg.eigh(A)\n",
    "print(v)\n",
    "print(P)\n",
    "print(np.matmul(np.matmul(P.T,A),P))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3ef40d9c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1. 2. 5.]\n",
      "[[ 0.      1.      0.    ]\n",
      " [-0.7071  0.      0.7071]\n",
      " [ 0.7071  0.      0.7071]]\n",
      "[[1. 0. 0.]\n",
      " [0. 2. 0.]\n",
      " [0. 0. 5.]]\n"
     ]
    }
   ],
   "source": [
    "#练习5-18\n",
    "import numpy as np\n",
    "from utility import symmetrization\n",
    "np.set_printoptions(precision=4, suppress=True)\n",
    "A=np.array([[2,0,0],\n",
    "            [0,3,4],\n",
    "            [0,0,3]])\n",
    "symmetrization(A)\n",
    "v,P=np.linalg.eigh(A)\n",
    "print(v)\n",
    "print(P)\n",
    "print(np.matmul(np.matmul(P.T,A),P))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e3eabb72",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-8. -5. -2.]\n"
     ]
    }
   ],
   "source": [
    "#例5-21\n",
    "import numpy as np                      #导入numpy\n",
    "from utility import symmetrization      #导入symmetrization\n",
    "A=np.array([[-5,4,4],                   #初始化A\n",
    "            [0,-6,0],\n",
    "            [0,0,-4]])\n",
    "symmetrization(A)                       #对称化A\n",
    "v=np.linalg.eigvalsh(A)                 #计算A的特征值\n",
    "print(v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "b3411091",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-1.  1.  2.]\n"
     ]
    }
   ],
   "source": [
    "#练习5-19\n",
    "import numpy as np                      #导入numpy\n",
    "from utility import symmetrization      #导入symmetrization\n",
    "A=np.array([[1,2,0],                   #初始化A\n",
    "            [0,0,-2],\n",
    "            [0,0,1]])\n",
    "symmetrization(A)                       #对称化A\n",
    "v=np.linalg.eigvalsh(A)                 #计算A的特征值\n",
    "print(v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "ba9a58f8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[9/5 -18/5 0]\n"
     ]
    }
   ],
   "source": [
    "#例5-23\n",
    "import numpy as np\n",
    "from utility import mySolve\n",
    "from utility import Q\n",
    "np.set_printoptions(formatter={'all':lambda x:\n",
    "                               str(Q(x).limit_denominator())})\n",
    "A=np.array([[4,2,-1],\n",
    "            [3,-1,2],\n",
    "            [11,3,0]],dtype='float')\n",
    "b=np.array([2,10,8])\n",
    "n,_=A.shape\n",
    "B=np.matmul(A.T,A)\n",
    "c=np.matmul(A.T,b.reshape(n,1))\n",
    "X=mySolve(B,c)\n",
    "print(X[:,0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "c577903b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 1/3 0 0]\n"
     ]
    }
   ],
   "source": [
    "#练习5-21\n",
    "import numpy as np\n",
    "from utility import mySolve\n",
    "from utility import Q\n",
    "np.set_printoptions(formatter={'all':lambda x:\n",
    "                               str(Q(x).limit_denominator())})\n",
    "A=np.array([[1,-2,3,-1],\n",
    "            [3,-1,5,-3],\n",
    "            [2,1,2,-2]],dtype='float')\n",
    "b=np.array([1,2,3])\n",
    "n,_=A.shape\n",
    "B=np.matmul(A.T,A)\n",
    "c=np.matmul(A.T,b.reshape(n,1))\n",
    "X=mySolve(B,c)\n",
    "print(X[:,0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4e77374d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
